package chap18.pds.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import jdbc.JdbcUtil;
import chap18.pds.model.PdsItem;

public class PdsItemDao {
	private static PdsItemDao instance = new PdsItemDao();
	public static PdsItemDao getInstance(){
		return instance;
	}
	
	private PdsItemDao(){}
	
	public int selectCount(Connection conn) throws SQLException{
		Statement stmt = null;
		ResultSet rs = null;
		
		try {
			stmt = conn.createStatement();
			rs = stmt.executeQuery("select count(*) from pds_item");
			rs.next();
			return rs.getInt(1);
		} finally{
			JdbcUtil.close(rs);
			JdbcUtil.close(stmt);
		}
	}
	
	public List<PdsItem> select(Connection conn, int firstRow, int endRow)
			throws SQLException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("select * from pds_item order by pds_item_id desc limit ?,?");
			ps.setInt(1, firstRow-1);
			ps.setInt(2, endRow-firstRow+1);
			rs = ps.executeQuery();
			if(!rs.next()){
				return Collections.emptyList();
			}
			List<PdsItem> itemList = new ArrayList<PdsItem>();
			do{
				PdsItem article = makeItemFromResultSet(rs);
				itemList.add(article);
			}while(rs.next());
			return itemList;
		} finally{
			JdbcUtil.close(rs);
			JdbcUtil.close(ps);
		}
	}

	private PdsItem makeItemFromResultSet(ResultSet rs) throws SQLException {
		PdsItem item = new PdsItem();
		item.setId(rs.getInt("pds_item_id"));
		item.setFileName(rs.getString("filename"));
		item.setRealPath(rs.getString("realpath"));
		item.setFileSize(rs.getLong("filesize"));
		item.setDownloadCount(rs.getInt("downloadcount"));
		item.setDescription(rs.getString("description"));
		return item;
	}
	
	public int insert(Connection conn, PdsItem item) throws SQLException{
		PreparedStatement ps = null;
		Statement st = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("insert into pds_item " +
				"(filename, realpath ,filesize, downloadcount, " +
				"description) value(?,?,?,0,?)");
			ps.setString(1, item.getFileName());
			ps.setString(2, item.getRealPath());
			ps.setLong(3, item.getFileSize());
			ps.setString(4, item.getDescription());
			int insertedCount = ps.executeUpdate();
			
			if(insertedCount>0){
				st = conn.createStatement();
				rs = st.executeQuery("select last_insert_id() from pds_item");
				if(rs.next()){
					return rs.getInt(1);
				}
			}
			return -1;
		} finally{
			JdbcUtil.close(rs);
			JdbcUtil.close(st);
			JdbcUtil.close(ps);
		}
	}

	public int increaseCount(Connection conn, int id) 
			throws SQLException{
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement("update pds_item " +
				"set downloadcount = downloadcount+1 " +
				"where pds_item_id=?");
			ps.setInt(1, id);
			return ps.executeUpdate();
		} finally{
			JdbcUtil.close(ps);
		}
	}

	public PdsItem selectById(Connection conn, int id)
			throws SQLException{
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("select * from pds_item " +
					"where pds_item_id=?");
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if(!rs.next()){
				return null;
			}
			PdsItem item = makeItemFromResultSet(rs);
			return item;
		} finally{
			JdbcUtil.close(rs);
			JdbcUtil.close(ps);
		}
	}
	
	
}
